package com.blueocean_health.care.management.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.blueocean_health.care.management.common.LocalManager;
import com.blueocean_health.care.management.domain.base.BuildFloorDept;
import com.blueocean_health.care.management.domain.base.FloorDept;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.base.User;
import com.blueocean_health.care.management.domain.dto.AddDeptDto;
import com.blueocean_health.care.management.domain.dto.BuildFloorDto;
import com.blueocean_health.care.management.domain.dto.DeptManagerDto;
import com.blueocean_health.care.management.domain.dto.PullDownDto;
import com.blueocean_health.care.management.em.LocalManagerType;
import com.blueocean_health.care.management.mapper.BuildFloorDeptMapper;
import com.blueocean_health.care.management.mapper.DepartmentMapper;
import com.blueocean_health.care.management.mapper.FloorDeptMapper;
import com.blueocean_health.care.management.mapper.OrderServiceInfoMapper;
import com.blueocean_health.care.management.service.DepartmentService;
import com.blueocean_health.care.management.service.PostageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 科室 业务实现类 2017年8月30日14:42:12
 * @author wuyanli
 *
 */
@Service
public class DepartmentServiceImpl implements DepartmentService{
	@Autowired
	private DepartmentMapper deptDao;
	
	@Autowired
	private FloorDeptMapper floorDeptDao;
	@Autowired
	private BuildFloorDeptMapper buildFloorDao;

	@Autowired
	private OrderServiceInfoMapper serviceDao;
	
	@Autowired
	private PostageService postService;
	
	@Override
	public List<PullDownDto> getDeptByHospitalId(String hospitalId) {
		List<PullDownDto> dept = deptDao.getDeptByHospitalId(hospitalId);
//		PullDownDto pull = new PullDownDto();
//		pull.setValue("999999");
//		pull.setName("通用科室");
//		dept.add(pull);
		return dept;
	}

	/**
	 * 根据筛选条件查询科室信息  2018年1月6日15:10:55
	 */
	@Override
	public PageInfo<DeptManagerDto> getDeptManagerList(Map<String, String> map) {
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		map.put("hospitalId", user.getHospitalId());
		String deptName = map.get("deptName");
		if(!StringUtils.isEmpty(deptName)){
			deptName = deptName.trim();
			map.put("deptName", deptName);
		}
		int pageSize = Integer.parseInt(map.get("pageSize"));
		int page = Integer.parseInt(map.get("page"));
		PageHelper.startPage(page,pageSize);
		List<DeptManagerDto> deptManagerList = deptDao.getDeptManagerList(map);
		//添加是否关联资费字段
		for (DeptManagerDto deptManagerDto : deptManagerList) {
			Map<String,String> map2 = new HashMap<>();
			map2.put("deptId", deptManagerDto.getDeptId());
			List<String> floorDeptIds = floorDeptDao.getFloorDeptId(map2);
			//科室信息只有一条
			if(floorDeptIds.size() == 1){
				//调接口
				boolean checkdeptId = postService.checkdeptId(deptManagerDto.getDeptId());
				deptManagerDto.setHasExpenses(checkdeptId);
			}else{
				//可以删除
				deptManagerDto.setHasExpenses(false);
			}
		}
		PageInfo<DeptManagerDto> pageInfo = new PageInfo<DeptManagerDto>(deptManagerList);
		
		return pageInfo;
	}
	/**
	 * 根据筛选条件导出科室信息  2018年1月9日18:02:52
	 */
	@Override
	public List<DeptManagerDto> deptListExcel(Map<String, String> map) {
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		map.put("hospitalId", user.getHospitalId());
		List<DeptManagerDto> deptManagerList = deptDao.getDeptManagerList(map);
		
		return deptManagerList;
	}

	@Override
	public AddDeptDto getDeptManagerById(Map<String, String> map) {
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		map.put("hospitalId", user.getHospitalId());
		List<DeptManagerDto> deptManagerById = deptDao.getDeptManagerById(map);
		if(StringUtils.isEmpty(deptManagerById)){
			return null;
		}
		AddDeptDto deptDto = new AddDeptDto();
		DeptManagerDto deptManagerDto2 = deptManagerById.get(0);
		deptDto.setStandardDept(deptManagerDto2.getStandardDept());
		deptDto.setDeptId(deptManagerDto2.getDeptId());
		deptDto.setDeptName(deptManagerDto2.getDeptName());
		deptDto.setNurseManager(deptManagerDto2.getNurseManager());
		List<BuildFloorDto> list = new ArrayList<>();
		for (DeptManagerDto deptManagerDto : deptManagerById) {
			BuildFloorDto build = new BuildFloorDto();
			build.setBuildingId(deptManagerDto.getBuildingId());
//			build.setBuildingName(null);
			build.setFloorId(deptManagerDto.getFloorId());
			Map<String,String> map1 = new HashMap<>();
			map1.put("floor", deptManagerDto.getFloorId());
			map1.put("department", map.get("deptId"));
			map1.put("buildingNumber", deptManagerDto.getBuildingId());
			List<String> orderDeptFlag = serviceDao.getOrderDeptFlag(map1);
			if(orderDeptFlag.size() > 0){
				build.setDeptFlag(false);
			}else{
				build.setDeptFlag(true);
			}
			list.add(build);
		}
		deptDto.setList(list);
		return deptDto;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public SysResult insertDept(AddDeptDto addDept) {
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		DeptManagerDto deptDto = new DeptManagerDto();
		//获取科室Id
		Map<String,String> map = new HashMap<>();
		map.put("hospitalId", user.getHospitalId());
		map.put("deptName", addDept.getDeptName());
		//核对改科室名称是否已存在
		List<DeptManagerDto> dto = deptDao.getDeptId(map);
		if(!StringUtils.isEmpty(dto) && dto.size() > 0){
			return SysResult.error(-1, "该科室已存在，请勿重复提交");
		}
		deptDto.setDeptName(addDept.getDeptName().trim());
		deptDto.setStandardDept(addDept.getStandardDept());
		deptDto.setHospitalId(user.getHospitalId());
		deptDto.setNurseManager(addDept.getNurseManager());
		deptDao.insertDept(deptDto);
		List<DeptManagerDto> deptDto2 = deptDao.getDeptId(map);
		String deptId = deptDto2.get(0).getDeptId();
		List<BuildFloorDto> list = addDept.getList();
		for (BuildFloorDto buildFloorDto : list) {
			//将数据保存到floor-dept中间表
			FloorDept floorDept = new FloorDept();
			floorDept.setDeptId(deptId);
			floorDept.setFloorId(buildFloorDto.getFloorId());
			floorDeptDao.insertDeptFloor(floorDept);
			//获取中间表Id
			Map<String,String> map1 = new HashMap<>();
			map1.put("floorId", buildFloorDto.getFloorId());
			map1.put("deptId", deptId);
			List<String> floorDeptId = floorDeptDao.getFloorDeptId(map1);
			//将数据保存到build-floor-dept中间表
			BuildFloorDept buildFloor = new BuildFloorDept();
			buildFloor.setBuildId(buildFloorDto.getBuildingId());
			buildFloor.setFloorDeptId(floorDeptId.get(0));
			buildFloor.setHospitalId(user.getHospitalId());
			buildFloorDao.insertBuildDeptFloor(buildFloor);
		}
		
		return SysResult.ok(0, "保存成功");
	}

	/**
	 * 修改科室信息  2018年1月9日16:58:10
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public SysResult updateDept(AddDeptDto addDept) {
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		DeptManagerDto deptDto = new DeptManagerDto();
		//获取科室Id
		Map<String,String> map = new HashMap<>();
		map.put("hospitalId", user.getHospitalId());
		map.put("deptName", addDept.getDeptName().trim());
		map.put("deptId", addDept.getDeptId());
		//核对改科室名称是否已存在
		List<DeptManagerDto> dto = deptDao.getDeptId(map);
		if(!StringUtils.isEmpty(dto) && dto.size() > 0){
			return SysResult.error(-1, "该科室已存在，请勿重复提交");
		}
		
		//修改科室信息
		deptDto.setDeptId(addDept.getDeptId());
		deptDto.setDeptName(addDept.getDeptName().trim());
		deptDto.setStandardDept(addDept.getStandardDept());
		deptDto.setHospitalId(user.getHospitalId());
		deptDto.setNurseManager(addDept.getNurseManager());
		deptDao.updateDept(deptDto);
		//删除老数据再重新插入
		//通过楼号和科室Id在floor-dept表查询出关联表Id并将其删除
//		map.put("floorId", addDept.getFloorId());
		List<String> floorDeptId2 = floorDeptDao.getFloorDeptId(map);
		for (String string : floorDeptId2) {
			String floorDeptId = string;
			floorDeptDao.deleteFloorDept(floorDeptId);
			//通过关联表Id和楼号Id将build-floor-dept的数据删除
//			map.put("buildId", addDept.getBuildId());     
			map.put("floorDeptId", floorDeptId);  
			
			buildFloorDao.deleteBuildFloorDept(map);
			
		}
		
		//重新插入要保存科室的 关联关系
		List<BuildFloorDto> list = addDept.getList();
		for (BuildFloorDto buildFloorDto : list) {
			//将数据保存到floor-dept中间表
			FloorDept floorDept = new FloorDept();
			floorDept.setDeptId(addDept.getDeptId());
			floorDept.setFloorId(buildFloorDto.getFloorId());
			floorDeptDao.insertDeptFloor(floorDept);
			//获取中间表Id
			Map<String,String> map1 = new HashMap<>();
			map1.put("floorId", buildFloorDto.getFloorId());
			map1.put("deptId", addDept.getDeptId());
			List<String> floorDeptIds = floorDeptDao.getFloorDeptId(map1);
			//将数据保存到build-floor-dept中间表
			BuildFloorDept buildFloor = new BuildFloorDept();
			buildFloor.setBuildId(buildFloorDto.getBuildingId());
			buildFloor.setFloorDeptId(floorDeptIds.get(0));
			buildFloor.setHospitalId(user.getHospitalId());
			buildFloorDao.insertBuildDeptFloor(buildFloor);
		}
		
		return SysResult.ok(0, "修改成功");
	}

	/**
	 * 删除科室信息 2018年1月9日16:30:24
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public SysResult deleteDept(DeptManagerDto deptDto) {
		//通过科室Id在floor-dept表查询所有数据如果size>1否则将科室信息也删除
		Map<String,String> map = new HashMap<>();
		map.put("deptId", deptDto.getDeptId());
		List<String> floorDeptIds = floorDeptDao.getFloorDeptId(map);
		if(floorDeptIds.size() == 0){
			return SysResult.error(-1, "没有查询到相应的数据信息");
		}
		Map<String,String> map1 = new HashMap<>();
		map1.put("floor", deptDto.getFloorId());
		map1.put("department", deptDto.getDeptId());
		map1.put("buildingNumber", deptDto.getBuildingId());
		List<String> orderDeptFlag = serviceDao.getOrderDeptFlag(map1);
		if(orderDeptFlag.size() > 0){
			return SysResult.error(-1, "该科室已关联订单,无法删除");
		}
		if(deptDto.isHasExpenses()){
			//删除资费信息
			postService.deletebydept(deptDto.getDeptId());
		}
		if(floorDeptIds.size() <= 1){
			//删除科室信息
			deptDao.deleteDept(deptDto.getDeptId());
		}
		//通过楼号和科室Id在floor-dept表查询出关联表Id并将其删除
		map.put("floorId", deptDto.getFloorId());
		List<String> floorDeptId2 = floorDeptDao.getFloorDeptId(map);
		String floorDeptId = floorDeptId2.get(0);
		floorDeptDao.deleteFloorDept(floorDeptId);
		//通过关联表Id和楼号Id将build-floor-dept的数据删除
		map.put("buildId", deptDto.getBuildingId());     
		map.put("floorDeptId", floorDeptId);     
		buildFloorDao.deleteBuildFloorDept(map);
		return SysResult.ok(0, "删除成功");
	}

}
